﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Table.DataModel;
using Table;
using UnityEngine.UI;

namespace UI
{
    public class AutoChessExplainPageHeroSkill : MonoBehaviour
    {

        public TextureKit skillIconTextureKit1;
        public TextureKit skillIconTextureKit2;
        public Text skillNameText;
        public Text skillDescriptionText;
        public Text skillConsumeText;
        public GameObject selectedIcon;
        public GameObject skillDetailPanel;
        public GameObject skillDetailPanelArrow;
        public GameObject star0;
        public GameObject star1;
        public GameObject star2;
        public GameObject selectCircle;

        public Transform detailPanelTransformTop;
        public Transform detailPanelTransformBottom;
        public Transform detailPanelArrowTransTop;
        public Transform detailPanelArrowTransBottom;
        public Transform detailPanelParent;
        public Transform viewPortTransform;
        public GameObject detailPanelMask;
        public ScrollRect scrollRect;
        public AutoScrollContent autoScrollText;

        private SkillData skillData;
        private string skillName;
        private string skillIcon;
        private int starLevel;

        public void SetContent(TableSkill skill, int starLevel)
        {
            var heroData = new HeroData();
            heroData.InitDictData(TableDataManager.Instance.tableHeroDao.GetById(skill.heroId));
            skillData = new SkillData(skill.skillId, heroData);
            skillName = skill.skillName;
            skillIcon = skill.skillIcon;

            this.starLevel = starLevel;
            skillIconTextureKit1.TextureName = skill.skillIcon;
        }

        public void OnClick()
        {
            detailPanelMask.SetActive(true);
            scrollRect.velocity = Vector3.zero;
            scrollRect.vertical = false;
            selectedIcon.SetActive(true);
            skillDetailPanel.SetActive(true);

            // 技能星星
            star0.SetActive(starLevel >= 1);
            star1.SetActive(starLevel >= 2);
            star2.SetActive(starLevel >= 3);

            skillIconTextureKit2.TextureName = skillIcon;
            skillNameText.text = skillName;
            skillDescriptionText.text = skillData.GetSkillDesc();
            skillConsumeText.text = string.Format(TableTextBlo.GetText("ACExplainPageHeroSubPageSkillDetailPanel"), UI.ArabicToCHSParser.ParseInt(starLevel));

            detailPanelTransformBottom.SetParent(detailPanelParent);

            float BottomRectTransformPosY = detailPanelTransformBottom.GetComponent<RectTransform>().anchoredPosition.y;
            float skillDetailPanelHeight = skillDetailPanel.GetComponent<RectTransform>().rect.height;
            float skillDetailPanelParentBottom = detailPanelParent.GetComponent<RectTransform>().rect.height / 2f;

            // 测试位置是否会让说明面板超出边界
            bool isBottom = BottomRectTransformPosY - skillDetailPanelHeight > -skillDetailPanelParentBottom;

            if (isBottom)
            {
                skillDetailPanel.transform.localPosition = detailPanelTransformBottom.localPosition;
                detailPanelTransformBottom.SetParent(viewPortTransform);

                detailPanelArrowTransBottom.SetParent(skillDetailPanel.transform);
                skillDetailPanelArrow.transform.localPosition = detailPanelArrowTransBottom.localPosition;
                skillDetailPanelArrow.transform.localRotation = Quaternion.identity;
                detailPanelArrowTransBottom.SetParent(transform);
            }
            else
            {
                detailPanelTransformTop.SetParent(detailPanelParent);
                skillDetailPanel.transform.localPosition = detailPanelTransformTop.localPosition;
                skillDetailPanel.transform.localPosition += new Vector3(0f, skillDetailPanelHeight, 0f);
                detailPanelTransformTop.SetParent(viewPortTransform);

                detailPanelArrowTransTop.SetParent(skillDetailPanel.transform);
                skillDetailPanelArrow.transform.localPosition = detailPanelArrowTransTop.localPosition;
                skillDetailPanelArrow.transform.localEulerAngles = new Vector3(180f, 0f, 0f);
                detailPanelArrowTransTop.SetParent(transform);
            }
            detailPanelTransformBottom.SetParent(viewPortTransform);
           
            skillDetailPanel.SetActive(true);

            autoScrollText.ResetToBeginning();

            // 添加隐藏说明面板的委托
            AutoChessHerosSubPage.OnHideDetailPanel = () =>
            {
                selectCircle.SetActive(false);
            };
        }
    }
}

